package com.vehicle.controller;

import com.vehicle.bean.DataGridView;
import com.vehicle.bean.PageVo;
import com.vehicle.bean.ResultDto;
import com.vehicle.pojo.ApplyUse;
import com.vehicle.pojo.Check;
import com.vehicle.service.ApplyUseService;
import com.vehicle.service.CarExamineService;
import com.vehicle.service.CarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
@RequestMapping("CarExamine")
public class CarExamineController extends DataTimeController{

    @Autowired
    private CarExamineService carExamineService;

    @Autowired
    private ApplyUseService applyUseService;

    //查询所有申请用车信息
    @RequestMapping("findAllApplyUse")
    @ResponseBody
    public DataGridView  findAllApplyUse(Integer deptid, PageVo pageVo){
        return carExamineService.findAllApplyUseByStatus(deptid,pageVo);
    }

    //跳转预览界面
    @RequestMapping("CarExamineCheck")
    public ModelAndView CarExamineCheck(Integer applyid){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/page/car/CarExamineCheck.jsp");
        ApplyUse applyUse = applyUseService.selectApplyUseById(applyid);
        modelAndView.addObject("applyUse",applyUse);
        return modelAndView;
    }

    //添加审批结果
    @RequestMapping("CheckAdd")
    @ResponseBody
    public ResultDto CheckAdd(Check check){
        ResultDto resultDto = new ResultDto();
        try {
            Date date = new Date();
            check.setChecktime(date);
            carExamineService.CheckAdd(check);
            if(check.getCheckresult()==1){
                applyUseService.updateApplyUseState(check.getApplyid(),2);
            }else if(check.getCheckresult()==2){
                applyUseService.updateApplyUseState(check.getApplyid(),4);
            }
            resultDto.setCode(1001);
            resultDto.setMsg("数据提交成功");
        }catch (Exception e){
         e.printStackTrace();
         resultDto.setCode(-1001);
         resultDto.setMsg("数据提交失败");
        }
        return resultDto;
    }

    //删除审批结果和申请
    @RequestMapping("delCheck")
    @ResponseBody
    public ResultDto delCheck(Integer applyid){
        ResultDto resultDto = new ResultDto();
        try {
            applyUseService.deApplyUse(applyid);
            carExamineService.delCheck(applyid);
        }catch (Exception e){
            e.printStackTrace();
        }
        return resultDto;
    }
}
